package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 何景航
 * @date 2025/11/12 10:43
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断购物车数据是否已存在
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        Long currentId = BaseContext.getCurrentId();
        System.out.println("当前用户id：" + currentId);
        cart.setUserId(currentId);

        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        //如果已存在，则数量加1
        if(list != null && list.size() > 0){
            ShoppingCart cartInDB = list.get(0);
            cartInDB.setNumber(cartInDB.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cartInDB);
        }else {
            //如果不存在，则添加到购物车，数量默认为1

            //判断加入的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (dishId != null) {
                //本次添加的是菜品
                Dish dish = dishMapper.getById(dishId);
                cart.setAmount(dish.getPrice());
                cart.setName(dish.getName());
                cart.setImage(dish.getImage());
            }else{
                //本次添加的是套餐
                Setmeal setmeal = setmealMapper.getById(setmealId);
                cart.setAmount(setmeal.getPrice());
                cart.setName(setmeal.getName());
                cart.setImage(setmeal.getImage());
            }
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(cart);
        }
    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart cart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        return list;
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    @Override
    public void deleteShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        cart.setUserId(userId);

        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        ShoppingCart cartInDB = list.get(0);
        if(cartInDB.getNumber() == 1){
            shoppingCartMapper.deleteById(cartInDB.getId());
        }else{
            cartInDB.setNumber(cartInDB.getNumber() - 1);
            shoppingCartMapper.updateNumberById(cartInDB);
        }
    }
}
